#第一题
class  User:
    user = {}

    def __init__(self,name, passwd, money):

        self.name = name
        self.passwd = passwd
        self.money = money


    def buy_goods(self, good, weight):

        if goods not in list(Goods.goodsdit.keys()):
            print("这地儿没得卖")

        elif float(weight) > Goods.goodsdit[good].store:
            print("不早点来没得了下次早点吧")
        else:
            pay = float(weight) * Goods.goodsdit[good].price
            if self.money >= pay:
                self.money -= pay
                Goods.goodsdit[good].store -= float(weight)
                print(f"购买成功，{self.name}剩余{self.money}元， "
                      f"{good}剩余库存{Goods.goodsdit[good].store}kg")
            else:
                print("余额不足")

    def login(self, newname, newpasswd, newmoney):
        if len(User.user) == 0:
            self.serialname = 0
        else:
            userkey = list(User.user.keys())
            self.serialname = userkey[-1] + 1
        newuser = User(newname, newpasswd, newmoney)
        newdict = {newuser.serialname: newuser}
        User.user.update(newdict)
        print(User.user)
    def register(self, serialname, name, passwd):
        # 登录的时候进行判断，3次机会

        for i in range(3):
            if i <= 2:
                i += 1
                print(list(User.user.keys()))

                if serialname in list(User.user.keys()):

                    if User.user[serialname].name == name and User.user[serialname].passwd == passwd:
                        print("登录成功")
                        break
                    else:
                        print("编号或密码错误还有{i}次机会")
                else:
                    print("请先注册")
                    break

class Goods:
    goodsdit = {}

    def __init__(self, classify, product_name, price, store):
            self.classify = classify
            self.product_name = product_name
            self.price = price
            self.store = store

    def addgoods(self, classify, product_name, price, store):
            newgoods = Goods(classify, product_name, price, store)
            newdict = {product_name: newgoods}
            Goods.goodsdit.update(newdict)



user0 = User.login(User, "daiyue", "999999999", 999999999)
user1 = User.login(User, "zhangjian", "123456", 5000)
user2 = User.login(User, "wangshenghu", "1234567", 8000)


apple = Goods.addgoods(Goods, "fruits", "apple", 8, 30)
orange = Goods.addgoods(Goods, "fruits", "orange", 4, 100)

while 1:
        print("欢迎来到三乐商店")
        select = input("选择登录、注册、退出\n")
        if select == "注册":
            newname = input("please input new name")
            newpasswd = input("please input new passwd")
            newmoney = input("please input new money")
            User.login(User, newname, newpasswd, newmoney)
            print("注册成功返回首页登录")


            print(list(User.user.keys()))
            continue

        elif select == "登录":
            serialname = int(input("please input serialname"))
            name = input("please input name")
            passwd = input("please input passwd")
            User.register(User, serialname, name, passwd)
            goods = input("请输入商品名称")
            weight = float(input("请输入重量(kg)"))
            User.buy_goods(User.user[serialname], goods, weight)

        elif select == "退出":
            print("不再看看了？再买点呗，再卖说不定打折")
            inp = input("不退出或退出")
            if inp == "不退出":
                print("哈哈")
            else:
                print("呵呵")
                break



#第二题
import copy


class Node:
    """节点类，包含值和下一个节点的指针"""

    def __init__(self, value, next=None):
        self.value = value
        self.next = next

    def __str__(self):
        return "Node value:%s" % self.value


class LinkedList:
    def __init__(self, head=None, tail=None):
        self.head = head
        self.tail = tail

    def get_all(self):
        """获取链表中所有的节点"""
        nodes = []
        temp = self.head
        while temp:
            nodes.append(temp.value)
            temp = temp.next
        return nodes

    def add_node(self, value):
        """在列表中添加节点"""
        print(self)
        node = Node(value)
        print(node)
        # 空链表,收尾指针都指向新增加的节点
        if self.head is None:
            self.head = node
            self.tail = node
        else:
            self.tail.next = node
            self.tail = node

    def reverse_list(self):
        """反转单向列表
        思路一：先对原链表做头删操作，再对新链表做头插
        """
        # 定义一个新的链表
        new_list_node = LinkedList()
        temp = copy.deepcopy(self.head)
        while temp:
            # 1.对之前的链表做头删除操作

            node = temp
            temp = temp.next

            # 2.对新的链表做头插入操作
            if new_list_node.head is None:
                new_list_node.tail = node
            node.next = new_list_node.head
            new_list_node.head = node
        return new_list_node


if __name__ == "__main__":
    l = LinkedList()
    for i in range(5):
        l.add_node(i)
    new_list_node = l.reverse_list()
    print(new_list_node.get_all())
    print(new_list_node.tail)


